#include "customWidgets.h"
#include <QPainter>
#include <iostream>
#include <QVBoxLayout>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>


///////////////////////*big btn*///////////////////////
BigBtn::BigBtn(QSize size, int radius, QWidget* parent) :
    QPushButton(parent),
    cornerRadius(radius) {

    this->setMinimumWidth(size.width());
    this->setMinimumHeight(size.height());
    this->setMaximumWidth(300);
    this->setMaximumHeight(300);
    this->resize(size);
    this->setStyleSheet(borderRadius + defaultColor);

    indicator = new QWidget(this);
    indicator->resize(this->width()/4, 6);
    indicator->move(0.375*this->width(), 0.875*this->height());
    indicator->setStyleSheet(indicatorDefaultStyle);

    QVBoxLayout* layout = new QVBoxLayout(this);
    this->setLayout(layout);
    layout->setContentsMargins(15, 35, 15, 35);
    layout->setSpacing(15);
//    layout->addWidget(indicator);
    layout->setAlignment(Qt::AlignCenter);
}

void BigBtn::enterEvent(QEvent* event) {
    this->setStyleSheet(borderRadius + hoverColor);

    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(0.3*this->width(),
                               0.875*this->height(),
                               this->width()*0.4,
                               6));
    longer->setDuration(100);
    longer->setEasingCurve(QEasingCurve::OutBack);
    longer->start();

    indicator->setStyleSheet(indicatorHoverStyle);
}

void BigBtn::leaveEvent(QEvent* event) {
    this->setStyleSheet(borderRadius + defaultColor);

    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(0.375*this->width(),
                               0.875*this->height(),
                               this->width()/4,
                               6));
    shorter->setDuration(150);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    shorter->start();
    indicator->setStyleSheet(indicatorDefaultStyle);
}

void BigBtn::mousePressEvent(QMouseEvent *event) {
    this->setStyleSheet(borderRadius + pressColor);
    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(0.1*this->width(),
                               0.875*this->height(),
                               this->width()*0.8,
                               6));
    longer->setDuration(100);
    longer->setEasingCurve(QEasingCurve::OutBack);
    longer->start();
}

void BigBtn::mouseReleaseEvent(QMouseEvent* event) {
    this->setStyleSheet(borderRadius + hoverColor);
    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(0.3*this->width(),
                               0.875*this->height(),
                               this->width()*0.4,
                               6));
    shorter->setDuration(100);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    shorter->start();
}

void BigBtn::resizeEvent(QResizeEvent* event) {
    indicator->resize(this->width()/4, 6);
    indicator->move(0.375*this->width(), 0.875*this->height());
}

///////////////////////*small btn*///////////////////////
SmallBtn::SmallBtn(QSize size, int radius, QWidget* parent) :
    QPushButton(parent),
    cornerRadius(radius) {
    this->resize(size);
    this->setStyleSheet(borderRadius + defaultColor);

    indicator = new QWidget(this);
    indicator->resize(2, indicatorHeight);
    indicator->move(this->width()/2 - 1, this->height() - indicatorHeight);
    indicator->setStyleSheet(indicatorDefaultStyle);
}
SmallBtn::SmallBtn(QSize size, QWidget* parent) :
    QPushButton(parent) {
    this->resize(size);
    this->setStyleSheet(borderRadius + defaultColor);

    indicator = new QWidget(this);
    indicator->resize(2, indicatorHeight);
    indicator->move(this->width()/2 - 1, this->height() - indicatorHeight);
    indicator->setStyleSheet(indicatorDefaultStyle);
}

void SmallBtn::enterEvent(QEvent* event) {
    this->setStyleSheet(borderRadius + hoverColor);

    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(0.4*this->width(),
                               this->height() - indicatorHeight,
                               this->width()*0.2,
                               indicatorHeight));
    longer->setDuration(100);
    longer->setEasingCurve(QEasingCurve::OutBack);
    longer->start();

    indicator->setStyleSheet(indicatorHoverStyle);
}

void SmallBtn::leaveEvent(QEvent* event) {
    this->setStyleSheet(borderRadius + defaultColor);

    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(this->width()/2 - 1,
                               this->height() - indicatorHeight,
                               2,
                               indicatorHeight));
    shorter->setDuration(150);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    shorter->start();
    indicator->setStyleSheet(indicatorDefaultStyle);
}

void SmallBtn::mousePressEvent(QMouseEvent *event) {
    this->setStyleSheet(borderRadius + pressColor);
    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(0.2*this->width(),
                               this->height() - indicatorHeight,
                               this->width()*0.6,
                               indicatorHeight));
    longer->setDuration(100);
    longer->setEasingCurve(QEasingCurve::OutBack);
    longer->start();
}

void SmallBtn::mouseReleaseEvent(QMouseEvent* event) {
    this->setStyleSheet(borderRadius + hoverColor);
    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(0.4*this->width(),
                                this->height() - indicatorHeight,
                                this->width()*0.2,
                                indicatorHeight));
    shorter->setDuration(100);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    shorter->start();
}

void SmallBtn::resizeEvent(QResizeEvent* event) {
    indicator->resize(2, indicatorHeight);
    indicator->move(this->width()/2 - 1, this->height() - indicatorHeight);
}


SelectionItem::SelectionItem(QString name, QString info, QWidget* parent) :
    QWidget(parent){
    //title
    QFont titleFont = QFont("Corbel", 13);
    QFontMetrics fm(titleFont);
    qreal height = fm.lineSpacing();
    title = new QLabel(this);
    title->setText(name);
    title->setFont(titleFont);
    title->setMinimumHeight(height);
    title->setStyleSheet("color:#2c2c2c");
    title->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
    //description
    QFont descFont = QFont("Corbel", 11);
    fm = QFontMetrics(descFont);
    height = fm.lineSpacing();
    description = new QLabel(this);
    description->setText(info);
    description->setFont(descFont);
    description->setMinimumHeight(height);
    description->setStyleSheet("color:#707070");
    description->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    //indicator
    indicator = new QWidget(this);
    indicator->resize(6, 0.4*this->height());
    indicator->move(4, 0.3*this->height());
    indicator->setStyleSheet("border-radius:3px; background-color:#0078D4");
    opac = new QGraphicsOpacityEffect(indicator);   //opacity
    opac->setOpacity(0);
    indicator->setGraphicsEffect(opac);

    //set selectionItem height
    setFixedHeight(title->height() + (info == "" ? 0 : description->height() + 5));
    //layout
    QVBoxLayout* contentLayout = new QVBoxLayout(this);
    contentLayout->setContentsMargins(20, 0, 0, 0);
    contentLayout->setSpacing(2);
    this->setLayout(contentLayout);
    contentLayout->addWidget(title);
    if (info != "") {
        contentLayout->addWidget(description);
    }
    contentLayout->setAlignment(Qt::AlignLeft | Qt::AlignCenter);

    //background widget
    bgWidget = new QWidget(this);
    bgWidget->resize(this->size());
    bgWidget->setStyleSheet("border-radius:5px; background-color:#00000000");
    bgWidget->lower();
    bgWidget->show();

    this->setMouseTracking(true);
}


void SelectionItem::enterEvent(QEvent* event) {
    bgWidget->setStyleSheet("border-radius:5px; background-color:#0a000000");
    QParallelAnimationGroup* enter = new QParallelAnimationGroup(this);
    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(4, 0.25*this->height(), 6, this->height()*0.5));
    longer->setDuration(150);
    longer->setEasingCurve(QEasingCurve::OutBack);
    QPropertyAnimation* fadeIn = new QPropertyAnimation(opac, "opacity", this);
    fadeIn->setStartValue(opac->opacity());
    fadeIn->setEndValue(0.99);
    fadeIn->setDuration(100);

    enter->addAnimation(longer);
    enter->addAnimation(fadeIn);
    enter->start();
}
void SelectionItem::leaveEvent(QEvent* event) {
    bgWidget->setStyleSheet("border-radius:5px; background-color:#00000000");
    QParallelAnimationGroup* leave = new QParallelAnimationGroup(this);
    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(4, 0.3 * this->height(), 6, 0.4 * this->height()));
    shorter->setDuration(150);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    QPropertyAnimation* fadeOut = new QPropertyAnimation(opac, "opacity", this);
    fadeOut->setStartValue(opac->opacity());
    fadeOut->setEndValue(onSelected ? 0.99 : 0);
    fadeOut->setDuration(100);

    leave->addAnimation(shorter);
    leave->addAnimation(fadeOut);
    leave->start();
}
void SelectionItem::mousePressEvent(QMouseEvent* event) {

}
void SelectionItem::mouseReleaseEvent(QMouseEvent* event) {

}
void SelectionItem::resizeEvent(QResizeEvent* event) {

}
